package ynu.edu.merchant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.edu.common.exception.BusinessException;
import ynu.edu.merchant.dto.MerchantCategoryDTO;
import ynu.edu.merchant.entity.MerchantCategory;
import ynu.edu.merchant.repository.MerchantCategoryRepository;
import ynu.edu.merchant.service.MerchantCategoryService;
import ynu.edu.merchant.vo.MerchantCategoryVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商家分类服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MerchantCategoryServiceImpl implements MerchantCategoryService {

    private final MerchantCategoryRepository categoryRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MerchantCategoryVO createCategory(MerchantCategoryDTO categoryDTO) {
        // 检查分类名称是否已存在
        if (categoryRepository.existsByName(categoryDTO.getName())) {
            throw new BusinessException("分类名称已存在");
        }

        // 创建分类实体
        MerchantCategory category = new MerchantCategory();
        BeanUtil.copyProperties(categoryDTO, category);

        // 保存分类
        category = categoryRepository.save(category);

        // 返回分类信息
        return convertToVO(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MerchantCategoryVO updateCategory(MerchantCategoryDTO categoryDTO) {
        // 检查分类是否存在
        MerchantCategory category = categoryRepository.findById(categoryDTO.getId())
                .orElseThrow(() -> new BusinessException("分类不存在"));

        // 检查分类名称是否已存在
        MerchantCategory existCategory = categoryRepository.findByName(categoryDTO.getName());
        if (existCategory != null && !existCategory.getId().equals(categoryDTO.getId())) {
            throw new BusinessException("分类名称已存在");
        }

        // 更新分类信息
        BeanUtil.copyProperties(categoryDTO, category, "id", "createTime", "updateTime", "isDeleted");

        // 保存分类
        category = categoryRepository.save(category);

        // 返回分类信息
        return convertToVO(category);
    }

    @Override
    public MerchantCategoryVO getCategoryById(Long id) {
        MerchantCategory category = categoryRepository.findById(id)
                .orElseThrow(() -> new BusinessException("分类不存在"));
        return convertToVO(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(Long id) {
        MerchantCategory category = categoryRepository.findById(id)
                .orElseThrow(() -> new BusinessException("分类不存在"));
        category.setIsDeleted(1);
        categoryRepository.save(category);
        return true;
    }

    @Override
    public List<MerchantCategoryVO> getAllCategories() {
        List<MerchantCategory> categories = categoryRepository.findByStatusAndIsDeletedOrderBySort(1, 0);
        return categories.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategoryStatus(Long id, Integer status) {
        MerchantCategory category = categoryRepository.findById(id)
                .orElseThrow(() -> new BusinessException("分类不存在"));
        category.setStatus(status);
        categoryRepository.save(category);
        return true;
    }

    /**
     * 将分类实体转换为VO
     *
     * @param category 分类实体
     * @return 分类VO
     */
    private MerchantCategoryVO convertToVO(MerchantCategory category) {
        MerchantCategoryVO categoryVO = new MerchantCategoryVO();
        BeanUtil.copyProperties(category, categoryVO);
        return categoryVO;
    }
} 